package com.example.controller;

import com.example.common.Result;
import com.example.entity.Goods;
import com.example.service.GoodsService;
import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 前端请求接口
 */
@ApiSupport(author = "wjp", order = 3)
@Tag(name = "每日推荐管理", description = "每日推荐的增删改查接口")
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Resource
    private GoodsService goodsService;

    /**
     * 新增
     */
    @PostMapping("/add")
    @Operation(summary = "新增每日推荐", description = "创建一条新的每日推荐")
    public Result add(@RequestBody Goods goods) {
        goodsService.add(goods);
        return Result.success();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    @Operation(summary = "修改每日推荐", description = "根据ID修改每日推荐内容")
    public Result update(@RequestBody Goods goods) {
        goodsService.updateById(goods);
        return Result.success();
    }

    @PutMapping
    @Operation(summary = "修改每日推荐", description = "根据ID修改每日推荐内容")
    public Result edit(@RequestBody Goods goods) {
        goodsService.edit(goods);
        return Result.success();
    }

    /**
     * 单个删除
     */
    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除每日推荐", description = "根据ID删除每日推荐")
    public Result delete(@Parameter(description = "每日推荐ID") @PathVariable Integer id) {
        goodsService.deleteById(id);
        return Result.success();
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/delete/batch")
    @Operation(summary = "批量删除每日推荐", description = "根据ID集合批量删除每日推荐")
    public Result delete(@RequestBody List<Integer> ids) {
        goodsService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 单个查询
     */
    @GetMapping("/selectById/{id}")
    @Operation(summary = "查询每日推荐", description = "根据ID查询每日推荐详情")
    public Result selectById(@Parameter(description = "每日推荐ID") @PathVariable Integer id) {
        Goods goods = goodsService.selectById(id);
        return Result.success(goods);
    }

    /**
     * 查询所有
     */
    @GetMapping("/selectAll")
    @Operation(summary = "查询所有每日推荐", description = "获取每日推荐列表，可根据条件查询")
    public Result selectAll(Goods goods) {
        List<Goods> list = goodsService.selectAll(goods);
        return Result.success(list);
    }

    /**
     * 分页查询
     */
    @GetMapping("/selectPage")
    @Operation(summary = "分页查询每日推荐", description = "分页获取每日推荐列表")
    public Result selectPage(Goods goods,
                             @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
                             @Parameter(description = "每页条数") @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Goods> pageInfo = goodsService.selectPage(goods, pageNum, pageSize);
        return Result.success(pageInfo);
    }

    @GetMapping("/recommend")
    @Operation(summary = "每日随机推荐", description = "获取每日推荐列表")
    public Result recommend() {
        List<Goods> list = goodsService.getRandomPictures();
        return Result.success(list);
    }


}
